#include "Terrain.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>



void initialiser_Terrain(Terrain *pTer)
{
	int x,y;

	const char terrain_defaut[11][38] = {
		"######################################",
		"######################################",
		"######################################",
		"######################################",
		"#######%######%#######%##%#######%####",
		":::::::::::::::::::::::::::::::::::::B",
		"##%#########%#####%#########%######%##",
		"######################################",
		"######################################",
		"######################################",
		"######################################",

    };

	pTer->dimx = 38;
	pTer->dimy = 11;

	pTer->tab = (char **)malloc(sizeof(char *)*pTer->dimy);
	for (y=0; y<pTer->dimy; y++)
		pTer->tab[y] = (char *)malloc(sizeof(char)*pTer->dimx);

	for(y=0;y<pTer->dimy;++y)
		for(x=0;x<pTer->dimx;++x)
			pTer->tab[y][x] = terrain_defaut[y][x];
}


void liberer_Terrain(Terrain *pTer)
{
	int y;

	for (y=0; y<pTer->dimy; y++)
		free(pTer->tab[y]);
	free(pTer->tab);

	pTer->dimx = 0;
	pTer->dimy = 0;
	pTer->tab = NULL;
}

int terPositionTourValide(const Terrain *pTer, const int x, const int y)
{
	if (x>=0 && x<pTer->dimx && y>=0 && y<pTer->dimy && pTer->tab[y][x] =='%' )
		return 1;
	else
		return 0;
}

const int getDimx(const Terrain *pTer)
{
	return pTer->dimx;
}

const int getDimy(const Terrain *pTer)
{
	return pTer->dimy;
}


const char terGetXY(const Terrain *pTer, const int x, const int y)
{
        assert( x>=0) ;
        assert( y>=0) ;
		/*assert( x<pTer->dimx ) ;
        assert( y<pTer->dimy ) ;*/
		return pTer->tab[y][x];
}

void terSetXY(Terrain *ter, const int x, const int y, char c)
{
        assert( x>=0) ;
        assert( y>=0) ;
        assert( x<ter->dimx ) ;
        assert( y<ter->dimy ) ;
		ter->tab[y][x] = c;
}

